<script setup lang="ts">
import { getMedicalOrderLogistics } from '@/services/order'
import type { Logistics } from '@/types/order'
import { ref } from 'vue'
import { onMounted } from 'vue'
import { useRoute } from 'vue-router'
import AMapLoader from '@amap/amap-jsapi-loader'
import startImg from '@/assets/start.png'
import endImg from '@/assets/end.png'
import carImg from '@/assets/car.png'

// 配置安全秘钥
window._AMapSecurityConfig = {
  securityJsCode: '415e917da833efcf2d5b69f4d821784b'
}

const route = useRoute()
const logistics = ref({} as Logistics)
onMounted(async () => {
  // 获取物流信息
  const res = await getMedicalOrderLogistics(route.params.id as string)
  logistics.value = res.data
  // 创建地图
  initMap(res.data)
})

// 地图初始化函数
const initMap = (data: Logistics) => {
  // console.log(data)
  AMapLoader.load({
    key: '4eed3d61125c8b9c168fc22414aaef7e', // 申请好的Web端开发者Key，首次调用 load 时必填
    version: '2.0' // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
  })
    .then((AMap) => {
      const map = new AMap.Map('map', {
        // 设置地图容器id
        // mapStyle: 'amap://styles/graffiti', // 地图样式
        zoom: 12 // 初始化地图展示比例
      })
      // 线路规划
      AMap.plugin('AMap.Driving', function () {
        var driving = new AMap.Driving({
          map: map,
          // 是否显示实时路况信息
          showTraffic: false,
          // 驾车路线规划策略，AMap.DrivingPolicy.LEAST_TIME是最快捷模式
          // policy: AMap.DrivingPolicy.LEAST_TIME
          // 隐藏标记点
          hideMarkers: true
        })

        // var startLngLat = [116.379028, 39.865042]
        // var endLngLat = [116.427281, 39.903719]

        const start = data.logisticsInfo.shift()
        const end = data.logisticsInfo.pop()

        driving.search(
          [start?.longitude, start?.latitude],
          [end?.longitude, end?.latitude],
          // [[经度, 纬度], [经度, 纬度]]
          {
            waypoints: data.logisticsInfo.map((item) => [item.longitude, item.latitude])
          },
          function (status: string, result: object) {
            // 未出错时，result即是对应的路线规划方案
          }
        )

        // 起点标记点
        const startMarker = new AMap.Marker({
          position: [start?.longitude, start?.latitude],
          icon: startImg,
          anchor: 'center'
        })
        map.add(startMarker)
        // 终点标记点
        const endMarker = new AMap.Marker({
          position: [end?.longitude, end?.latitude],
          icon: endImg,
          anchor: 'center'
        })
        map.add(endMarker)
        // 货车标记点
        const carMarker = new AMap.Marker({
          position: [data.currentLocationInfo.longitude, data.currentLocationInfo.latitude],
          icon: carImg,
          anchor: 'center'
        })
        map.add(carMarker)

        // 设置货车所在位置的地图缩放比例，达到最佳的视野效果
        setTimeout(() => {
          map.setFitView(
            [carMarker], // 覆盖物数组
            false, // 动画过渡到制定位置
            [60, 60, 60, 60], // 周围边距，上、下、左、右
            10 // 最大 zoom 级别
          )
        }, 2000)
      })
    })
    .catch((e) => {
      console.log(e)
    })
}
</script>

<template>
  <div class="order-logistics-page">
    <div id="map">
      <div class="title">
        <van-icon name="arrow-left" @click="$router.back()" />
        <span>{{ logistics.statusValue }}</span>
        <van-icon name="service" />
      </div>
      <div class="current">
        <p class="status">{{ logistics.statusValue }} 预计{{ logistics.estimatedTime }}送达</p>
        <p class="predict">
          <span>{{ logistics.name }}</span>
          <span>{{ logistics.awbNo }}</span>
        </p>
      </div>
    </div>
    <div class="logistics">
      <p class="title">物流详情</p>
      <van-steps direction="vertical" :active="0">
        <van-step v-for="item in logistics.list" :key="item.id">
          <p class="status">{{ item.statusValue }}</p>
          <p class="content">{{ item.content }}</p>
          <p class="time">{{ item.createTime }}</p>
        </van-step>
      </van-steps>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.order-logistics-page {
  --van-step-icon-size: 18px;
  --van-step-circle-size: 10px;
}
#map {
  height: 450px;
  background-color: var(--cp-bg);
  overflow: hidden;
  position: relative;
  .title {
    background-color: #fff;
    height: 46px;
    width: 355px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    padding: 0 15px;
    font-size: 16px;
    position: absolute;
    left: 10px;
    top: 10px;
    box-sizing: border-box;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    > span {
      flex: 1;
      text-align: center;
    }
    .van-icon {
      font-size: 18px;
      color: #666;
      &:last-child {
        color: var(--cp-primary);
      }
    }
  }
  .current {
    height: 80px;
    border-radius: 4px;
    background-color: #fff;
    height: 80px;
    width: 355px;
    box-sizing: border-box;
    padding: 15px;
    position: absolute;
    left: 10px;
    bottom: 10px;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    .status {
      font-size: 15px;
      line-height: 26px;
    }
    .predict {
      color: var(--cp-tip);
      font-size: 13px;
      margin-top: 5px;
      > span {
        padding-right: 10px;
      }
    }
  }
}
.logistics {
  padding: 0 10px 20px;
  .title {
    font-size: 16px;
    padding: 15px 5px 5px;
  }
  .van-steps {
    :deep(.van-step) {
      &::after {
        display: none;
      }
    }
    .status {
      font-size: 15px;
      color: var(--cp-text3);
      margin-bottom: 4px;
    }
    .content {
      font-size: 13px;
      color: var(--cp-tip);
      margin-bottom: 4px;
    }
    .time {
      font-size: 13px;
      color: var(--cp-tag);
    }
  }
}
</style>
